using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Templates_SearchTemplate : BaseTemplate
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Master.FixTopicImage("~/img/search_results.jpg");
    }

    protected void dlArticles_DataBinding(object sender, EventArgs e)
    {
        Acms.WebControls.AcmsDataList dlArticles = sender as Acms.WebControls.AcmsDataList;
        Acms.WebControls.AcmsDataListItem item = dlArticles.NamingContainer as Acms.WebControls.AcmsDataListItem;
        int topicId = 0;
        if (dlTopics.DataKeys[item.ItemIndex] != null && dlTopics.DataKeys[item.ItemIndex].Value is int)
        {
            topicId = (int)dlTopics.DataKeys[item.ItemIndex].Value;
        }
        if (topicId > 0)
        {
            int totalRecords = 0;
            dlArticles.DataSource = Acms.Bll.Search.GetAllByTopicAndSearchTerm(topicId, searchText, dlArticles.PageSize * dlArticles.CurentPageIndex, dlArticles.PageSize, ref totalRecords);
            dlArticles.TotalRecords = totalRecords;
        }
    }

    void SearchButton_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            SearchText = Master.SearchTextBox.Text;
            Acms.Bll.SearchTerm tmp = Acms.Bll.SearchTerm.CreateInstance(CurrentSiteId, SearchText);
            tmp.Update();
            phSeach.Visible = true;
            phNoText.Visible = false;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SearchText = Request.Form[Master.SearchTextBox.UniqueID];
            if (string.IsNullOrEmpty(SearchText))
            {
                SearchText = Request.QueryString["Text"];
            }
            if(string.IsNullOrEmpty(searchText) || searchText.Length < 3)
            {
                phNoText.Visible = true;
                phSeach.Visible = false;
            }
            else
            {
                Master.SearchTextBox.Text = SearchText;
                Acms.Bll.SearchTerm tmp = Acms.Bll.SearchTerm.CreateInstance(CurrentSiteId, SearchText);
                tmp.Update();
                phNoText.Visible = false;
                phSeach.Visible = true;
            }
        }
    }

    private string searchText;
    protected string SearchText
    {
        get { return searchText; }
        set
        {
            if (!string.IsNullOrEmpty(value))
            {
                Master.SetTopicTitle(string.Format("Search : '{0}'", value));
            }
            else
            {
                Master.SetTopicTitle("Search");
            }
            searchText = value;
        }
    }

    protected override object SaveViewState()
    {
        ViewState["SearchText"] = searchText ;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        searchText = ViewState["SearchText"] as string;
    }

    protected void dsTopics_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Topic> e)
    {
        e.SetData(Acms.Bll.Topic.GetAllBySiteId(CurrentPost.SiteId));
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SearchText = txtSearch.Text;
            Master.SearchTextBox.Text = SearchText;
            phSeach.Visible = true;
            phNoText.Visible = false;
        }
    }

    protected void dlArticles_PageIndexChanged(object sender, EventArgs e)
    {
        ((Acms.WebControls.AcmsDataList)sender).SelectedIndex = -1;
        ((Acms.WebControls.AcmsDataList)sender).DataBind();
    }

    protected void dlArticles_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (Acms.WebControls.AcmsDataListItem item in dlTopics.Items)
        {
            Acms.WebControls.AcmsDataList dlArticles = item.FindControl("dlArticles") as Acms.WebControls.AcmsDataList;
            if (dlArticles.SelectedIndex != -1 && dlArticles != sender as Acms.WebControls.AcmsDataList)
            {
                dlArticles.SelectedIndex = -1;
                dlArticles.DataBind();
            }
        }
        ((Acms.WebControls.AcmsDataList)sender).DataBind();
    }
}
